﻿using B.S.Meeting.Domain;
using B.S.Meeting.ErrorCode;
using B.S.Meeting.WriteAPI.Applications.Commands.MeetingRooms;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Numerics;

namespace B.S.Meeting.WriteAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MeetingRoomController : ControllerBase
    {
        IMediator mediator;
        ILogger<MeetingRoomController> logger;

        public MeetingRoomController(IMediator mediator, ILogger<MeetingRoomController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 添加会议室信息
        /// </summary>
        /// <param name="room">要添加的会议室信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> AddRoom(MeetingRoom room)
        {
            try
            {
                AddRoomCommand command = new AddRoomCommand
                {
                    Data = room
                };
                logger.LogInformation($"成功添加会议室: {room.MeetingRoomName} (ID: {room.MeetingRoomId})");
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "添加会议室时发生错误");
                throw;
            }
        }

        /// <summary>
        /// 删除会议室
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> DelRoom(int meetinroomId)
        {
            try
            {
                logger.LogInformation($"尝试删除会议室: ID: {meetinroomId}");
                DelRoomCommand cmd = new DelRoomCommand
                {
                    MeetingRoomId = meetinroomId
                };
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "删除会议室时发生错误");
                return new APIResult<int>
                {
                    Code = StatusEnumCode.Fail,
                    Message = "删除会议室失败"
                };
            }
        }

        /// <summary>
        /// 修改会议室
        /// </summary>
        /// <param name="meetingroom"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> Update(MeetingRoom meetingroom)
        {
            UpdRoomCommand cmd = new UpdRoomCommand
            {
                Data = meetingroom,
            };
            var res = await mediator.Send(cmd);
            return res;
        }

        /// <summary>
        /// 批量删除会议室
        /// </summary>
        /// <param name="meetingroomIds"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> BatchDelRoom(List<int> meetingroomIds)
        {
            try
            {
                BatchDelRoomCommand cmd=new BatchDelRoomCommand
                {
                    MeetingRoomIds = meetingroomIds
                };
                logger.LogInformation($"尝试批量删除会议室: IDs: {string.Join(", ", meetingroomIds)}");
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
